package fregat.base.util;

import java.util.ResourceBundle;

/**
 * Class that wraps character based data.
 * As telegrams has fixed length all data items must use placeholders if no data
 * is set. For character base data ' ' is used as placeholder and data is
 * aligned right per default.
 *
 * @author Alexander Mann <br><b><i>ECO_L O G</i> &nbsp Logistiksysteme GmbH</b>
 * @version 1.0
 */
public class AlphaData {
  private String data; // data string
  private int characters; // number of characters
  private char placeholder = ' ';
  private int alignment;
  public static final int ALIGN_RIGHT = 0;
  public static final int ALIGN_LEFT = 1;

  /**
   * Creates character data with the given string and the number of
   * characters. If string is longer than given length it is stored with full
   * length but only a substring is returned by <code>toString()</code>
   *
   * @param data       Input string
   * @param characters Number of characters
   * @param placeholder Placeholder character for not used characters
   * @param alignment Alignment of text
   */
  public AlphaData(String data, int characters, char placeholder, int alignment) {
    this(data, characters);
    this.placeholder = placeholder;
    this.alignment = alignment;
  }

  /**
   * Creates character data with the given string and the number of
   * characters. If string is longer than given length it is stored with full
   * length but only a substring is returned by <code>toString()</code>
   *
   * @param data       Input string
   * @param characters Number of characters
   */
  public AlphaData(String data, int characters) {
    setData(data);
    this.characters = characters;
  }

  /**
   * Creates character data.
   *
   * @param data       Input string
   * @param start      Start position of substring
   * @param characters Length of substring
   * @param placeholder Placeholder character for not used characters
   * @param alignment Alignment of text
   */
  public AlphaData(String data, int start, int characters, char placeholder, int alignment) {
    this.characters = characters;
    this.placeholder = placeholder;
    this.alignment = alignment;
    parse(data,start);
  }

  /**
   * Creates character data.
   *
   * @param data       Input string
   * @param start      Start position of substring
   * @param characters Length of substring
   */
  public AlphaData(String data, int start, int characters) {
    this.characters = characters;
    parse(data,start);
  }

  /**
   * Creates character data by using the given string's length as number
   * of characters.
   *
   * @param data Input string
   */
  public AlphaData(String data) {
    this(data, data.length());
  }

  private void parse(String input, int start) {
    try {
      int firstCharacter = -1;
      int lastCharacter = -1;

      if(alignment == ALIGN_LEFT) {
        firstCharacter = start;
        int i=start+characters-1;
        while(lastCharacter <0 && i>=start) {
          if(input.charAt(i) != placeholder) {
            lastCharacter = i+1;
          }
          i--;
        }
      } else {
        lastCharacter = start+characters;
        int i=start;
        while(firstCharacter <0 && i<start+characters) {
          if(input.charAt(i) != placeholder) {
            firstCharacter = i;
          }
          i++;
        }
      }

      if(firstCharacter < 0 || lastCharacter < 0) {
        setData("");
      } else {
        setData(input.substring(firstCharacter,lastCharacter));
      }

    } catch (Exception ex) {
      Logger.getDefaultCategory()
        .warn("AscData(...) - " + ResourceBundle.getBundle("at.ecolog.base.util.resource")
          .getString("AlphaData_constructor_1") + " (" + input + ")," + start + "," + characters);
    }
  }

  /**
   * Sets the character data. If string is longer than number of characters set
   * when creating object only a substring is returned by <code>toString()</code>.
   *
   * @param data
   */
  public void setData(String data) {
    this.data = data;
  }

  /**
   * @return Character data without placeholders.
   */
  public String getData() {
    return data;    
  }

  /**
   * @return Character data or placeholder with the given number of characters
   */
  public String toString() {
    StringBuffer buffer = new StringBuffer();
    if(data != null) {
      buffer.append(data);
    }
    while (buffer.length() < characters) {
      if (alignment == ALIGN_LEFT) {
        buffer.append(placeholder);
      } else {
        buffer.insert(0, placeholder);
      }
    }
    return buffer.toString().substring(0, characters);
  }

  /**
   * Compares the character data to another instance.
   *
   * @param data Another <code>AlphaData</code> instance
   * @return True if equal
   */
  public boolean equals(AlphaData data) {
    return this.data.equalsIgnoreCase(data.toString());
  }

  /**
   * @return Character used as placeholder
   */
  public char getPlaceholder() {
    return placeholder;
  }

  /**
   * @return Alignment of data (returns constants <code>ALIGN_LEFT,ALIGN_RIGHT</code>)
   */
  public int getAlignment() {
    return alignment;
  }
}
